<template>
  <div>
    <div>
      <van-goods-action>
        <van-goods-action-icon icon="chat-o" text="客服" />
        <van-goods-action-icon icon="cart-o" text="购物车" />
        <van-goods-action-button
          type="warning"
          @click="shouwItem"
          text="加入购物车"
        />
        <van-goods-action-button
          type="danger"
          @click="shouwItem"
          text="立即购买"
        />
      </van-goods-action>
    </div>
    <div>
      <van-action-sheet class="danda" v-model="flag">
        <span class="close" @click="shouwItem">x</span>
        <div class="content">
          <van-card
            v-show="color"
            price="1.00"
            desc="剩余 50 件"
            title="已选红色"
            thumb="https://img01.yzcdn.cn/vant/ipad.jpeg"
          />
          
          <van-card
            v-show="!color"
            price="1.00"
            desc="剩余 50 件"
            title="已选蓝色"
            thumb="https://img01.yzcdn.cn/vant/ipad.jpeg"
          />

          <div
            v-for="(item, index) in list"
            :key="item.id"
            @click="changecolor(index)"
            :class="{ bgk: temp === index }"
            class="flaot"
          >
            <div class="divimg"><img src="" alt="" /></div>
            <div class="divcolor">{{ item.name }}</div>
          </div>

          <!-- <div @click="changecolor" class="flaot">
            <div class="divimg"><img src="" alt="" /></div>
            <div class="divcolor">蓝色</div>
          </div> -->

          <div class="wwwww">
            <div class="totlediv">购买数量</div>
            <van-stepper class="totol" v-model="value" />
          </div>
          <div>
            <van-goods-action>
              <van-goods-action-button @click="addcar" type="warning" text="加入购物车" />
              <van-goods-action-button  @click="addcar" type="danger" text="立即购买" />
            </van-goods-action>
          </div>
        </div>
      </van-action-sheet>
    </div>
  </div>
</template>

<script>
import { Notify ,Toast} from 'vant';
export default {
  data() {
    return {
      list: [
        { id: 1, name: "红色" },
        { id: 2, name: "蓝色" },
      ],
      color: true,
      flag: false,
      value: 8,
      temp: 3,
      dataId:0,
      listdata:[]
    };
  },
  methods: {
    shouwItem() {
      this.flag = !this.flag;
    },
    changecolor(index) {
      this.temp = index;
      this.color = !this.color;
    },
    addcar(){
      this.dataId=this.$route.params.id
      if(this.temp!=0&&this.temp!=1){
        Notify({ type: 'warning', message: '请选择颜色' });
        return
      }
      this.$router.push('/car')
      this.addgetprodcut()
      

    },

    async addgetprodcut(){
      const res=await this.$require({
        url:'/cart/add',
        method:'post',
        data:{
           productId :this.dataId,
          count:this.value
        }
      })
      if(res.status==200){
        this.listdata=res.config.data
        Toast.success('添加成功');
        return
      }else{
        Toast.fail('添加失败');
      }
      }
  },
};
</script>

<style lang="scss" scoped>
*{
  padding: 0;
  margin: 0;
}
.danda {
  width: 375px;
  height: 342px;
}
.close {
  margin-left: 350px;
  font-size: 30px;
  opacity: 0.5;
}

.content {
  // margin-top: 30px;
  width: 375px;
  height: 300px;
  .bigbox {
    padding: 12px;
    .colordiv {
      color: #000000;
      font-size: 14px;
      width: 343px;
      height: 31px;
      padding-bottom: 0 0 12px;
    }
  }

  .flaot {
    margin-top: 20px;
    float: left;
    margin-left: 5px;
    .divimg {
      float: left;
      width: 34px;
      height: 24px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .divcolor {
      float: left;
      width: 42px;
      height: 32px;
      text-align: center;
    }
  }

  .wwwww {
    padding: 70px 0;
    width: 375px;
    .totol {
      width: 100px;
      color: #000000;
      font-size: 14px;
      float: left;
      // margin-left: 50px;
    }

    .totlediv {
      width: 270px;
      float: left;
    }
  }
  .bgk {
    background-color: azure;
    background-color: antiquewhite;
  }
}
</style>